const express = require('express')
const request = require('request')
const router = express.Router()

const postparams = {
    "auth": {
     "identity": {
      "methods": [
       "password"
      ],
      "password": {
       "user": {
        "domain": {
         "name": ""
        },
        "name": "",
        "password": ""
       }
      }
     }
    }
   }

router.post('/getHuaweiYunToken',(req,res) => {
    postparams.auth.identity.password.user.domain.name = req.body.iam
    postparams.auth.identity.password.user.name = req.body.username
    postparams.auth.identity.password.user.password = req.body.password
    request(
        {
            url:'https://iam.myhuaweicloud.com/v3/auth/tokens',
            method: "POST",
            json: true,
            headers: {
                "content-type": "application/json",
            },
            body:postparams
        },
        (error,response,body) => {
                return res.status(response.statusCode).json(response)
            // if (error || (response.statusCode !== 200 && response.statusCode !== 201) ) {
            //     return res.status(500).json({type:'error',message:{'error':error,'response':response,'body':body}})
            // }
            // res.send({"body":body,"huaweiToken":response.headers["x-subject-token"]})
        }
    )
})


// 向设备发送消息
const postmsg2 = {
    "message":{
        "liq":"134"
    }
}

router.post('/postIotMessage',(req,res) => {
    // console.log(req.headers['huawei-token']);
    request(
        {
            url:`https://a1619f3acd.iotda.cn-north-4.myhuaweicloud.com/v5/iot/0e38e3e87000f3712f02c01759a3018b/devices/65fa5f10fb8177243a500abb_test123/messages`,
            method:'POST',
            json: true,
            headers:{
                "content-type": "application/json",
                "X-Auth-Token":req.headers["huawei-token"]
            },
            body:postmsg2
        },
        (error,response,body) => {
            return res.status(response.statusCode).json(body)
            // if (error || (response.statusCode !== 200 && response.statusCode !== 201) ) {
            //     return res.status(500).json({type:'error',message:{'error':error,'response':response,'body':body}})
            // }
            // res.send({"body": body})
        }
    )
})

// 向设备发送消息
const postmsg3 = {
  "service_id" : "BasicData",
  "command_name" : "lightControl",
  "paras" : {
    "value" : "ON"
  }
}

router.post('/postIotCommond',(req,res) => {
    // console.log(req.headers);
    request(
        {
            url:`https://a1619f3acd.iotda.cn-north-4.myhuaweicloud.com/v5/iot/0e38e3e87000f3712f02c01759a3018b/devices/65fa5f10fb8177243a500abb_test123/commands`,
            method:'POST',
            json: true,
            headers:{
                "content-type": "application/json",
                "X-Auth-Token":req.headers["huawei-token"]
            },
            body:postmsg3
        },
        (error,response,body) => {
            return res.status(response.statusCode).json(body)

            // if (error || (response.statusCode !== 200 && response.statusCode !== 201) ) {
            //     return res.status(500).json({type:'error',message:{'error':error,'response':response,'body':body}})
            // }
            // res.send({"body": body})
        }
    )
})


module.exports = router